package com.wlz.rocketmq.ordermessage;

import com.wlz.rocketmq.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.List;

/**
 *  顺序消息
 * @author wlz
 * @date 2022-07-01  10:36 下午
 */
@Slf4j
public class Producer {

    public static void main(String[] args) throws Exception{
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name_5");

        producer.setNamesrvAddr(Constants.NAMESVADDR);

        producer.start();

        for (int i = 0; i < 10; i++) {
            int orderId = i;
            for (int j = 0; j < 5; j++) {
                Message msg = new Message("OrderTopicTest", "order_" + orderId,
                        ("order_" + orderId + " step " + j).getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult send = producer.send(msg, new MessageQueueSelector() {
                    @Override
                    public MessageQueue select(List<MessageQueue> mqs, Message message, Object o) {
                        Integer id = (Integer) o;
                        int index = id % mqs.size();
                        return mqs.get(index);
                    }
                }, orderId);

                log.info(" SendResult : {}", send);
            }
        }

        producer.shutdown();


    }
}
